package me.zhengjie.ship.shipsoftbase.service.impl;

import me.zhengjie.ship.shipsoftbase.domain.ShipSoftBase;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.ship.shipsoftbase.repository.ShipSoftBaseRepository;
import me.zhengjie.ship.shipsoftbase.service.ShipSoftBaseService;
import me.zhengjie.ship.shipsoftbase.service.dto.ShipSoftBaseDto;
import me.zhengjie.ship.shipsoftbase.service.dto.ShipSoftBaseQueryCriteria;
import me.zhengjie.ship.shipsoftbase.service.mapper.ShipSoftBaseMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author liwh
* @date 2020-04-22
*/
@Service
//@CacheConfig(cacheNames = "shipSoftBase")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class ShipSoftBaseServiceImpl implements ShipSoftBaseService {

    private final ShipSoftBaseRepository shipSoftBaseRepository;

    private final ShipSoftBaseMapper shipSoftBaseMapper;

    public ShipSoftBaseServiceImpl(ShipSoftBaseRepository shipSoftBaseRepository, ShipSoftBaseMapper shipSoftBaseMapper) {
        this.shipSoftBaseRepository = shipSoftBaseRepository;
        this.shipSoftBaseMapper = shipSoftBaseMapper;
    }

    @Override
    //@Cacheable
    public Map<String,Object> queryAll(ShipSoftBaseQueryCriteria criteria, Pageable pageable){
        Page<ShipSoftBase> page = shipSoftBaseRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(shipSoftBaseMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<ShipSoftBaseDto> queryAll(ShipSoftBaseQueryCriteria criteria){
        return shipSoftBaseMapper.toDto(shipSoftBaseRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public ShipSoftBaseDto findById(Integer id) {
        ShipSoftBase shipSoftBase = shipSoftBaseRepository.findById(id).orElseGet(ShipSoftBase::new);
        ValidationUtil.isNull(shipSoftBase.getId(),"ShipSoftBase","id",id);
        return shipSoftBaseMapper.toDto(shipSoftBase);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public ShipSoftBaseDto create(ShipSoftBase resources) {
        return shipSoftBaseMapper.toDto(shipSoftBaseRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(ShipSoftBase resources) {
        ShipSoftBase shipSoftBase = shipSoftBaseRepository.findById(resources.getId()).orElseGet(ShipSoftBase::new);
        ValidationUtil.isNull( shipSoftBase.getId(),"ShipSoftBase","id",resources.getId());
        shipSoftBase.copy(resources);
        shipSoftBaseRepository.save(shipSoftBase);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            shipSoftBaseRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<ShipSoftBaseDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ShipSoftBaseDto shipSoftBase : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("版本名称", shipSoftBase.getVersionName());
            map.put("版本", shipSoftBase.getVersion());
            map.put("软件状态", shipSoftBase.getVersionState());
            map.put("备注", shipSoftBase.getRemarks());
            map.put("开始时间", shipSoftBase.getStartTime());
            map.put("结束时间", shipSoftBase.getEndTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}